﻿'Interface créer par Caroline St-Yves
'Programmez par Caroline St-Yves
'Date de création : 5 septembre 2012, 8h00 am.
'Date dernière modification : 11 octobre 2012, 23h50. 'effectué par Caroline
Imports System.Data.SqlClient
Public Class GestionPoste
    Dim selectID As Integer
    Dim oLocalContext As DBMainContext
    Public oListPoste As New List(Of Poste)
    Sub loadDG()
        oLocalContext = New DBMainContext
        Try
            Dim evensQuery = From u In oLocalContext.Poste
                             Where Not oListPoste.Contains(u)
                             Select u

            Dim array() = evensQuery.ToArray()

            dgvPoste.DataSource = evensQuery
            dgvPoste.Columns(0).Visible = False
            dgvPoste.Columns(2).Visible = False

        Catch ex As Exception
            Console.Write(ex.Message)
            Console.Write("Tous les postes ont été trouvés!")
        End Try

    End Sub

    Private Sub GestionPoste_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        oLocalContext = New DBMainContext
        Try
            Dim evensQuery = From u In oLocalContext.Poste
                             Where Not oListPoste.Contains(u)
                             Select u

            Dim array() = evensQuery.ToArray()

            dgvPoste.DataSource = evensQuery
            dgvPoste.Columns(0).Visible = False
            dgvPoste.Columns(2).Visible = False

        Catch ex As Exception
            Console.Write(ex.Message)
            Console.Write("Tous les postes ont été trouvés!")
        End Try

    End Sub

    Private Sub btnSauvegarder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSauvegarder.Click
        oLocalContext.CustomSubmitChanges()
    End Sub
    Private Sub dgvFormation_RowValidated(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvPoste.RowValidated
        Try
            oLocalContext.CustomSubmitChanges()

        Catch sqlExp As SqlException

            If (sqlExp.Number = 547) Then

                MsgBox("Ce poste ne peut être supprimé, car il est lié à des enregistrements", MsgBoxStyle.Critical)
                CType(Me.Parent, CustomMessageBox).Close()

            End If

        End Try
    End Sub
    Public Function GetSelectedItems() As List(Of Poste)
        Return (From u As DataGridViewRow In dgvPoste.SelectedRows Select CType(u.DataBoundItem, Poste)).ToList

    End Function

    Private Sub btnAjoutPoste_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAjoutPoste.Click
        dgvPoste.Rows(dgvPoste.Rows.Count - 1).Selected = True
    End Sub

    Private Sub btnSupprimer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSupprimer.Click
        Dim answer As DialogResult
        answer = MessageBox.Show("Voulez-vous supprimer ce poste?", "Yes ou Non", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If answer = vbYes Then

            Dim deletePoste = (From cust In oLocalContext.Formation
                       Where cust.ID = selectID).ToList()

            oLocalContext.Formation.DeleteOnSubmit(deletePoste(0))
            loadDG()

            Try
                oLocalContext.SubmitChanges()
            Catch
                ' Handle exception.
            End Try
        End If
    End Sub

    Private Sub dgvPoste_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvPoste.CellContentClick
        selectID = dgvPoste.Rows(dgvPoste.SelectedCells(0).RowIndex).Cells(0).Value
    End Sub
End Class
